package cc.yugu.yhdw.webserver.web.controller.security.basic;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.TerminalCode;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.basic.TerminalCodeService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/security/basic/terminal_code")
public class TerminalCodeController extends SecurityController {
    @Autowired
    TerminalCodeService terminalCodeService;

    @RequestMapping(value = "index.htm")
    public void index(Model model, HttpSession session) {
//        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_02_01.getValue());
    }

    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(TerminalCode search, HttpSession session) {
        search.setAgentId(getSessionUser(session).getAgentId());
        return PageResult.successResult(terminalCodeService.findPage(search));
    }

    @RequestMapping(value = "add.htm")
    public void add(Model model) {
    }

    @RequestMapping(value = "swap.htm", method = RequestMethod.GET)
    public String swap(Model model, String id) {
        TerminalCode entity = terminalCodeService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/basic/terminal_code/swap";
    }

    @RequestMapping(value = "swap.htm", method = RequestMethod.POST)
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult swap(String from, String to) {
        return terminalCodeService.swap(from, to);
    }
}
